﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessDA;
using CognisessWeb.Models;
using CognisessService;

namespace CognisessWeb.Controllers
{
    public class MenuController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            List<MenusModel> menus = new List<MenusModel>();
            List<Menu> listmenu = new List<Menu>();            
            listmenu = accountBLL.GetMenu(new Menu { });
            foreach (var item in listmenu)
            {
                MenusModel menu = new MenusModel();
                menu.Id = item.Id;
                menu.MenuContent = item.MenuContent;
                menu.SubprimeTitle = item.SubprimeTitle;
                menu.SubprimeContent = item.SubprimeContent;
                menu.Title = item.Title;
                menu.Navigations = new List<NavigationModel>{};
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation{Id = menu.Title});
                for (int i = 0; i < navigations.Count; i++)
                {
                    menu.Navigations.Add(new NavigationModel { Id = menu.Title,TitleName = navigations[i].TitleName });
                }
                menus.Add(menu);
            }
            return View(menus);
        }

        //
        // GET: /Menu/Details/5

        public ActionResult Details(int id)
        {
            List<MenusModel> menus = new List<MenusModel>();
            List<Menu> listmenu = new List<Menu>();
            Menu m = new Menu();
            m.Id = id;
            listmenu = accountBLL.GetMenu(m);
            foreach (var item in listmenu)
            {
                MenusModel menu = new MenusModel();
                menu.Id = item.Id;
                menu.Title = item.Title;
                menu.MenuContent = item.MenuContent;
                menu.SubprimeTitle = item.SubprimeTitle;
                menu.SubprimeContent = item.SubprimeContent;
                menu.Navigations = new List<NavigationModel> { };
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation { Id = menu.Title });
                for (int i = 0; i < navigations.Count; i++)
                {
                    menu.Navigations.Add(new NavigationModel { Id = menu.Title, TitleName = navigations[i].TitleName });
                }
                menus.Add(menu);
            }
            return View(menus);
        }

        //
        // GET: /Menu/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Menu/Create

        [HttpPost]
        public ActionResult Create(Menu collection)
        {
            try
            {
                // TODO: Add insert logic here
                List<Menu> menus = new List<Menu>();
                menus.Add(collection);
                bool a = accountBLL.AddMenu(menus);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Menu/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<MenusModel> menus = new List<MenusModel>();
            List<Menu> listmenu = new List<Menu>();
            Menu m = new Menu();
            m.Id = id;
            listmenu = accountBLL.GetMenu(m);
            MenusModel menu = new MenusModel();
            foreach (var item in listmenu)
            {                
                menu.Id = item.Id;
                menu.Title = item.Title;
                menu.MenuContent = item.MenuContent;
                menu.SubprimeTitle = item.SubprimeTitle;
                menu.SubprimeContent = item.SubprimeContent;
                menu.Navigations = new List<NavigationModel> { };
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation { Id = menu.Title });
                for (int i = 0; i < navigations.Count; i++)
                {
                    menu.Navigations.Add(new NavigationModel { Id = menu.Title, TitleName = navigations[i].TitleName });
                }
            }
            return View(menu);
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Menu collection)
        {
            try
            {
                // TODO: Add update logic here

                List<Menu> munus = new List<Menu>();
                munus.Add(collection);                
                accountBLL.UpdataBlog(munus);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu/Delete/5
 
        public ActionResult Delete(int id)
        {
            //accountBLL.DeleteMunu(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Menu/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Menu collection)
        {
            try
            {
                // TODO: Add delete logic here
                //accountBLL.DeleteMunu(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
